home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / c / fat_string.c < prev    next >
C/C++ Source or Header  |  1990-08-13  |  14KB  |  562 lines

  1. /*
  2. (c) Copyright W. Schelter 1988, All rights reserved.
  3. */
  4.  
  5. /* 16 bit strings  with leader, and raw slots in the leader t_fat_string */
  6.  
  7.  
  8. #include "include.h"
  9. #include "page.h"
  10. #define FAT_STRING
  11.  
  12. object  Sfat_string;
  13. enum type what_to_collect;
  14. #define    inheap(pp)    ((char *)(pp) < heap_end)
  15.  
  16.  
  17.  
  18. /* start fasdump stuff */
  19. #include "fasdump.c"
  20.  
  21. /* this will be used for lines, and for structures that require some
  22. raw storage */
  23.  
  24. #define check_fs_args(ar,ind) \
  25.     if (type_of(ar) != t_fat_string) FEerror("Not a vector with leader",0); \
  26.    if ((ind >= (ar->fs.fs_dim)) \
  27.        ||(ind < 0)) FEerror("subscript out of bounds",0)
  28.    
  29. check_type_fat_string(p)
  30. object *p;
  31. {
  32. BEGIN:
  33.     if (type_of(*p)==t_fat_string) return;
  34.     *p = wrong_type_argument(Sfat_string, *p);
  35.     goto BEGIN;
  36.       }
  37.       
  38.  
  39. void
  40. siLfsref()
  41. { check_arg(2);
  42.   {register int  ind = fix(vs_base[1]);
  43.    register object ar =  vs_base[0];
  44.    check_fs_args(ar,ind);
  45.    vs_base[0]=make_fixnum((int) (ar->fs.fs_self[ind]));
  46.    vs_top=vs_base+1;}}
  47.  
  48. void
  49. siLfsset()
  50. {register object *base,ar;
  51.  register int ind;
  52.  check_arg(3);
  53.  base=vs_base;
  54.  ar=base[0];
  55.  check_type_integer(&base[1]);
  56.  ind =fix(base[1]); 
  57.  check_fs_args(ar,ind);
  58.  base[0]=base[2];
  59.  ar->fs.fs_self[ind]=fix(base[0]);
  60.  vs_top=base+1;
  61. }
  62.  
  63. #define check_fs_leader(ar,ind) \
  64.       if (type_of(ar) != t_fat_string) FEerror("Not a vector with leader",0); \
  65.       if ((ind >= ar->fs.fs_leader_length)||(ind < 0)) FEerror("subscript out of bounds",0)
  66.  
  67.  
  68. fs_leader_ref(ar,ind)
  69.      register object ar;
  70.      register int ind;
  71. { check_arg(2);
  72.   check_fs_leader(ar,ind);
  73.   return (int) fs_leader(ar,ind);
  74. }
  75.  
  76.  
  77. void
  78. check_raw(raw,i)
  79.      unsigned int raw;
  80.      int i;
  81. {if (!(raw & (1 << i))) FEerror("Slot not raw",0);}
  82.  
  83.  
  84. void
  85. siLfs_leader_ref()
  86. { register object *base;
  87.   base=vs_base;
  88.   base[0]=(object) fs_leader_ref(base[0],fix(base[1]));
  89.   vs_top=base+1;
  90. }
  91.  
  92. void
  93. siLfixnum_fs_leader_ref()
  94. { register object *base;
  95.   base=vs_base;
  96.   check_raw((base[0]->fs.fs_raw),fix(base[1]));
  97.   base[0]=make_fixnum(fs_leader_ref(base[0],fix(base[1])));
  98.   vs_top=base+1;
  99. }
  100.  
  101.  
  102. void
  103. fs_leader_set(ar,ind,val)
  104.  register object ar;
  105.   register  int ind;
  106.      object val;
  107. {  check_arg(3);
  108.   check_fs_leader(ar,ind);
  109.   fs_leader(ar,ind)=  val;
  110. }
  111.  
  112.  
  113. void
  114. siLfs_leader_set()
  115. { register object *base;
  116.   base=vs_base;
  117.   fs_leader_set(base[0],fix(base[1]), base[2]);
  118.   base[0]=base[2];
  119.   vs_top=base+1;}
  120.  
  121. void
  122. siLfixnum_fs_leader_set()
  123. { register object *base;
  124.   base=vs_base;
  125.   check_type_integer(&base[2]);
  126.   check_raw((base[0])->fs.fs_raw,fix(base[1]));
  127.   fs_leader_set(base[0],fix(base[1]),(object) fix(base[2]));
  128.   base[0]=base[2];
  129.   vs_top=base+1;}
  130.  
  131. void
  132. mark_fat_string(x)
  133.      object x;
  134. {register char *cp;
  135.  { int i=0,raw=x->fs.fs_raw;
  136.    cp = (char *) x->fs.fs_self;
  137.    while (i < x->fs.fs_leader_length)
  138.      {if (raw & 1) ;
  139.       else mark_object(fs_leader(x,i));
  140.       raw=( raw >> 1); i++;
  141.     }}
  142.  {int leader_size = (x->fs.fs_leader_length) * sizeof(object *);
  143.   int body_size = leader_size + (x->fs.fs_dim)*sizeof(fatchar);
  144.   cp=cp-leader_size;
  145.   if ((int)what_to_collect >= (int)t_contiguous) {
  146.     if (inheap(cp)) {
  147.       if (what_to_collect == t_contiguous)
  148.     mark_contblock(cp,body_size);
  149.     }
  150.     else x->fs.fs_self =
  151.       (fatchar *) ((char *)copy_relblock(cp,body_size) + leader_size);
  152.   }}}
  153.    
  154. void
  155. siLfs_array_total_size()
  156. {vs_top=vs_base+1;
  157.   check_type_fat_string(&vs_base[0]);
  158.  vs_base[0]=make_fixnum(vs_base[0]->fs.fs_dim);
  159. }
  160.  
  161. void
  162. siLfs_fill_pointer()
  163. {vs_top=vs_base+1;
  164.   check_type_fat_string(&vs_base[0]);
  165.  vs_base[0]=make_fixnum(vs_base[0]->fs.fs_fillp);
  166. }
  167.  
  168.  
  169. void
  170. siLset_fs_fill_pointer()
  171. {check_arg(2);
  172.  check_type_fat_string(&vs_base[0]);
  173.  vs_top=vs_base+1;
  174.  vs_base[0]->fs.fs_fillp = fix(vs_base[1]);
  175. }
  176.  
  177.  
  178. object
  179. make_fat_string(dim,raw,lleng,staticp)
  180. int dim,raw,lleng;
  181. {object x;
  182.  x=alloc_object(t_fat_string);
  183.  vs_push(x);
  184.  x->fs.fs_dim=dim;
  185.  x->fs.fs_raw=raw;
  186.  x->fs.fs_leader_length=lleng;
  187.  x->fs.fs_fillp=0;
  188.  alloc_fs(x,staticp);
  189.  return x;
  190. }
  191.  
  192. void
  193. siLmake_fat_string()
  194. { register object *base;
  195.  check_arg(4);
  196.  base=vs_base;
  197.  base[0]=make_fat_string (fix(base[0]),fix(base[1]),fix(base[2]),
  198.               (base[3]!=Cnil));
  199.  vs_top=base+1;
  200. }
  201.  
  202.  
  203. alloc_fs(x,staticp)
  204. object x; int staticp;
  205. {char *cp, *actual_cp ;
  206.  register object *obp;
  207.  char *(*f)();
  208.  int leader_size=sizeof(object *)*(x->fs.fs_leader_length);
  209.  if (staticp)
  210.    f = alloc_contblock;
  211.  else
  212.    f = alloc_relblock;
  213.  obp=(object *)(cp= (*f)(sizeof(fatchar) * (x->fs.fs_dim)
  214.             +leader_size));
  215.  actual_cp=cp+leader_size;
  216.  while(obp <  (object *) actual_cp)
  217.    {*obp=Cnil;
  218.     obp++;}
  219.  x->fs.fs_self=(fatchar *)actual_cp;
  220. }
  221.  
  222.  
  223. object siLprofile_array;
  224.  
  225. void
  226. siLprofile() /*(start-address scale) where scale is 0 <= n <= 256 */
  227. {
  228. object ar=siLprofile_array->s.s_dbind;
  229. if (type_of(ar)!=t_string)
  230.       FEerror("si:*Profile-array* not a string",0);
  231. if((vs_top-vs_base != 2) ||
  232.    type_of(vs_base[0])!=t_fixnum ||   type_of(vs_base[1])!=t_fixnum)
  233.      FEerror("Needs start address and scale as args",0);
  234.   profil((char *) (ar->ust.ust_self), (ar->ust.ust_dim),
  235.        fix(vs_base[0]),fix(vs_base[1]) << 8);
  236. }
  237.  
  238. void
  239. siLfunction_start()
  240. {check_arg(1);
  241.  if(type_of(vs_base[0])!=t_cfun) FEerror("not compiled function",0);
  242.  vs_base[0]=make_fixnum((int) (vs_base[0]->cf.cf_self));
  243. }
  244.  
  245. /* begin fasl stuff*/
  246.  
  247. #include "ext_sym.h"
  248. #ifdef AIX3
  249. #include <sys/ldr.h>
  250. char *data_load_addr =0;
  251. #endif
  252.  
  253. read_special_symbols(symfile)
  254. char *symfile;
  255. {FILE *symin;
  256.  char *symbols;
  257.  int i,jj;
  258.  struct lsymbol_table tab;
  259. #ifdef AIX3
  260.  {char buf[500];
  261.   struct ld_info * ld;
  262.  loadquery(L_GETINFO,buf,sizeof(buf));
  263.   ld = (struct ld_info *)buf;
  264.   data_load_addr = ld->ldinfo_dataorg ;}
  265. #endif  
  266.  if (!(symin=fopen(symfile,"r")))
  267.    {perror(symfile);exit(1);};
  268.  if(!fread((char *)&tab,sizeof(tab),1,symin))
  269.    FEerror("No header",0);
  270.  symbols=malloc(tab.tot_leng);
  271.  c_table.alloc_length=( (PTABLE_EXTRA+ tab.n_symbols));
  272.  (c_table.ptable) = (TABL *) malloc(sizeof(struct node) * c_table.alloc_length);
  273.  if (!(c_table.ptable)) {perror("could not allocate"); exit(1);};
  274.  i=0; c_table.length=tab.n_symbols;
  275.  while(i < tab.n_symbols)
  276.    { fread((char *)&jj,sizeof(int),1,symin);
  277. #ifdef FIX_ADDRESS
  278.      FIX_ADDRESS(jj);
  279. #endif       
  280.      (SYM_ADDRESS(c_table,i))=jj;
  281.      SYM_STRING(c_table,i)=symbols;
  282.  
  283.      while( *(symbols++) =   getc(symin)) 
  284.        {;}
  285. /*     dprintf( name %s ,  SYM_STRING(c_table,i));
  286.      dprintf( addr %d , jj);
  287. */
  288.      i++;
  289.    }
  290.  
  291.  /*
  292.    for(i=0;i< 5;i++)
  293.    {printf("Symbol: %d %s %d \n",i,SYM_STRINGN(c_table,i),
  294.    SYM_ADDRESS(*ptable,i));}
  295.    */
  296.  if (symin) fclose(symin);
  297. }
  298.  
  299. node_compare(node1,node2)
  300. char *node1, *node2;
  301. { return(strcmp( ((struct node *)node1)->string,
  302.              ((struct node *)node2)->string));}
  303.  
  304.  
  305. void
  306. siLread_externals()
  307. {check_arg(1);
  308.  {object x=vs_base[0];
  309.   unsigned int n;
  310.   char *str;
  311.   n=x->st.st_fillp;
  312.  check_type_string(&x);
  313.  str=malloc(n+1);
  314.   str[n]=NULL;
  315.  (void) strncpy(str,x->st.st_self,n);
  316.  read_special_symbols(str);
  317.   /* we sort them since these are used by the sfasl loader too */
  318. qsort((char*)(c_table.ptable),(int)(c_table.length),sizeof(struct node),node_compare);
  319.  free(str);}}
  320.  
  321. #define CFUN_LIM 10000
  322.  
  323. int maxpage;
  324. object siLcdefn;
  325.  
  326. #define CF_FLAG (1 << 31) 
  327.  
  328.  
  329. cfuns_to_combined_table(n) /* non zero n will ensure new table length */
  330. unsigned int n;
  331. {int ii=0;  
  332.  STATIC int i, j;
  333.  STATIC object x;
  334.  STATIC char *p,*cf_addr;
  335.  STATIC struct typemanager *tm;
  336.  if (! (n || combined_table.ptable)) n=CFUN_LIM;
  337.  if (n && combined_table.alloc_length < n)
  338.    { 
  339.      (combined_table.ptable)=NULL;
  340.      (combined_table.ptable)= (TABL *)malloc(n* sizeof(struct node));
  341.      if(!combined_table.ptable)
  342.        FEerror("unable to allocate",0);
  343.      combined_table.alloc_length=n;}
  344.  
  345.  for (i = 0;  i < maxpage;  i++) {
  346.    if ((enum type)type_map[i]!=tm_table[(short)t_cfun].tm_type &&
  347.        (enum type)type_map[i]!=tm_table[(short)t_gfun].tm_type &&
  348.        (enum type)type_map[i]!=tm_table[(short)t_sfun].tm_type &&
  349.        (enum type)type_map[i]!=tm_table[(short)t_vfun].tm_type
  350.        )
  351.      continue;
  352.    tm = tm_of((enum type)type_map[i]);
  353.    p = pagetochar(i);
  354.    for (j = tm->tm_nppage; j > 0; --j, p += tm->tm_size) {
  355.      x = (object)p;
  356.      if (type_of(x)!=t_cfun &&
  357.      type_of(x)!=t_sfun &&
  358.      type_of(x)!=t_vfun &&
  359.      type_of(x)!=t_gfun
  360.      ) continue;
  361.      if ((x->d.m == FREE) || x->cf.cf_self == NULL)
  362.        continue;
  363.     /* the cdefn things are the proclaimed call types. */
  364.      cf_addr=(char * ) ((unsigned int)(x->cf.cf_self));
  365.     
  366.      SYM_ADDRESS(combined_table,ii)=(unsigned int)cf_addr;
  367.      SYM_STRING(combined_table,ii)= (char *)(CF_FLAG | (unsigned int)x) ;
  368. /*       (x->cf.cf_name ? x->cf.cf_name->s.st_self : NULL) ; */
  369.      combined_table.length = ++ii;
  370.      if (ii >= combined_table.alloc_length)
  371.        FEerror("Need a larger combined_table",0);
  372.    }
  373.         
  374.  }
  375. }
  376.  
  377. address_node_compare(node1,node2)
  378. char *node1, *node2;
  379. {unsigned int a1,a2;
  380.  a1=((struct node *)node1)->address;
  381.  a2=((struct node *)node2)->address;
  382.  if (a1> a2) return 1;
  383.  if (a1< a2) return -1;
  384.  return 0;
  385. }
  386.  
  387.  
  388. void
  389. siLset_up_combined()
  390. {unsigned int n=0;
  391.  if (((vs_top - vs_base) == 1)&&type_of(vs_base[0])==t_fixnum)
  392.    n = (unsigned int) fix(vs_base[0]);
  393.  cfuns_to_combined_table(n);
  394.  if (c_table.ptable)
  395.    {int j,k;
  396.     if((k=combined_table.length)+c_table.length >=
  397.        combined_table.alloc_length)
  398.       cfuns_to_combined_table(combined_table.length+c_table.length +20);
  399.     for(j = 0; j < c_table.length;)
  400.     { SYM_ADDRESS(combined_table,k) =SYM_ADDRESS(c_table,j);
  401.       SYM_STRING(combined_table,k) =SYM_STRING(c_table,j);
  402.       k++;j++;
  403.     };
  404.     combined_table.length += c_table.length ;}
  405.  qsort((char*)combined_table.ptable,(int)combined_table.length,
  406.        sizeof(struct node),address_node_compare);
  407. }
  408.  
  409. static int  prof_start;
  410. prof_ind(address,scale)
  411.      unsigned int address;
  412. {address = address - prof_start ;
  413.  if (address > 0) return ((address * scale) >> 8) ;
  414.  return 0;
  415. }
  416.  
  417. /* sum entries AAR up to DIM entries */
  418. string_sum(aar,dim)
  419. register unsigned char *aar;
  420. unsigned int dim;
  421. {register unsigned char *endar;
  422.  register unsigned int count = 0;
  423. endar=aar+dim;
  424.  for ( ; aar< endar; aar++)
  425.    count += *aar;
  426.  return count;
  427. }
  428.  
  429. void
  430. siLdisplay_profile()
  431. {if (!combined_table.ptable)
  432.    FEerror("must symbols first",0);
  433.    check_arg(2);
  434.    {unsigned int prev,next,upto,dim,total;
  435.     int j,scale,count;
  436.     unsigned char *ar;
  437.     object obj_ar;
  438.     obj_ar=siLprofile_array->s.s_dbind;
  439.     if (type_of(obj_ar)!=t_string)
  440.       FEerror("si:*Profile-array* not a string",0);
  441.     ar=obj_ar->ust.ust_self;
  442.     scale=fix(vs_base[1]);
  443.     prof_start=fix(vs_base[0]);
  444.     vs_top=vs_base;
  445.     dim= (obj_ar->ust.ust_dim);
  446.  
  447.     total=string_sum(ar,dim);
  448.   
  449.     j=0;
  450.     {int i, finish = combined_table.length-1;
  451.      for(i =0,prev=SYM_ADDRESS(combined_table,i); i< finish;
  452.      prev=next)
  453.        { ++i;
  454.      next=SYM_ADDRESS(combined_table,i);
  455.      if ( prev < prof_start) continue;
  456.      upto=prof_ind(next,scale);
  457.      if (upto >= dim) upto=dim;
  458.      {char *name; unsigned int uname;
  459.       count=0;
  460.       for( ; j<upto;j++)
  461.         count += ar[j];
  462.       if (count > 0) {
  463.         name=SYM_STRING(combined_table,i-1);
  464.         uname = (unsigned int) name;
  465.         printf("\n%6.2f%% (%5d): ",(100.0*count)/total, count);
  466.         fflush(stdout);
  467.         if (CF_FLAG & uname)
  468.           {if (~CF_FLAG & uname) prin1( ((object) (~CF_FLAG & uname))->cf.cf_name,Cnil);}
  469.          else if (name ) printf("%s",name);};
  470.       if (upto==dim) goto TOTALS ;
  471.       
  472.     }}}
  473.  TOTALS:
  474.   printf("\nTotal ticks %d",total);fflush(stdout);
  475. }}
  476.  
  477. #ifdef SFASL
  478. int build_symbol_table();
  479. #endif
  480.  
  481.  
  482. /* end fasl stuff*/
  483.  
  484.  
  485. /* These are some low level hacks to allow determining the address
  486.    of an array body, and to allow jumping to inside the body
  487.    of the array */
  488.  
  489. siLarray_adress()
  490. {check_arg(1);
  491.  vs_base[0]=make_fixnum((int) (&(vs_base[0]->st.st_self[0])));
  492. }
  493.  
  494. /* This is some very low level code for hacking invokation of
  495.    m68k instructions in a lisp array.  The index used should be
  496.    a byte index.  So invoke(ar,3) jmps to byte ar+3.
  497.    */
  498.  
  499. #ifdef CLI
  500.  
  501. invoke(ar)
  502. char *ar;
  503. {asm("movel a6@(8),a0");
  504.  asm("jmp a0@");
  505. }
  506. /* save regs (2 3 4 5 6 7  10 11 12 13 14) and invoke restoring them */
  507. save_regs_invoke(ar)
  508. char *ar;
  509. {asm("moveml #0x3f3e,sp@-");
  510.  invoke(ar);
  511.  asm("moveml a6@(-44),#0x7cfc");
  512. }
  513. siLsave_regs_invoke()
  514. {int x;
  515.  check_arg(2);
  516.   check_type_integer(&vs_base[1]);
  517.   x=save_regs_invoke((vs_base[0]->st.st_self)+fix(vs_base[1]));
  518.  vs_top=vs_base+1;
  519.  vs_base[0]=make_fixnum(x);
  520. }
  521.  
  522.  
  523. #endif
  524.  
  525. init_fat_string()
  526. {make_si_function("ARRAY-ADDRESS",siLarray_adress);
  527. #ifdef CLI
  528.  make_si_function("SAVE-REGS-INVOKE",siLsave_regs_invoke);
  529. #endif 
  530.  make_si_function("FSREF",siLfsref);
  531.  make_si_function("FSSET",siLfsset);
  532.  make_si_function("FS-LEADER-REF",siLfs_leader_ref);
  533.  make_si_function("FS-LEADER-SET",siLfs_leader_set);
  534.  make_si_function("FIXNUM-FS-LEADER-SET",siLfixnum_fs_leader_set);
  535.  make_si_function("FIXNUM-FS-LEADER-REF",siLfixnum_fs_leader_ref);
  536.  make_si_function("SET-FS-FILL-POINTER",siLset_fs_fill_pointer);
  537.  make_si_function("FS-ARRAY-TOTAL-SIZE",siLfs_array_total_size);
  538.  make_si_function("FS-FILL-POINTER",siLfs_fill_pointer);
  539.  make_si_function("MAKE-FAT-STRING",siLmake_fat_string);
  540.  make_si_function("FUNCTION-START",siLfunction_start);
  541.  make_si_function("PROFILE",siLprofile);
  542.  make_si_function("READ-EXTERNALS",siLread_externals);
  543.  make_si_function("SET-UP-COMBINED",siLset_up_combined);
  544.  make_si_function("DISPLAY-PROFILE",siLdisplay_profile);
  545.  make_si_constant("*ASH->>*",(-1==(((int)-1) >> 50))? Ct :Cnil);
  546. #ifdef SFASL
  547.  make_si_function("BUILD-SYMBOL-TABLE",build_symbol_table);
  548. #endif
  549.  siLprofile_array=make_si_special("*PROFILE-ARRAY*",Cnil);
  550.  Sfat_string = make_ordinary("FAT-STRING");
  551.  enter_mark_origin(&Sfat_string);
  552.  init_fasdump();
  553.  
  554. }
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.